#!/bin/bash
#
# 银河麒麟操作系统-系统工具集
# 
# Author: rechie
# Email: gaoruiqi@kylinos.cn
# Date: 2022-02-11

#set -u -o pipefail

# 获取当前文件路径
CURPATH=$(dirname "$(readlink -f "$0")")

# 字体颜色
ERRCOLOR=$(tput setaf 1)
SUCCOLOR=$(tput setaf 2)
WARNCOLOR=$(tput setaf 3)
INFCOLOR=$(tput setaf 6)
NOCOLOR=$(tput sgr0)
QUECOLOR=$(tput setaf 1; tput bold)

# 限制必须有root权限才能执行脚本
if [ "$(id -u)" -ne 0 ];then
    printf '%s\n' "$ERRCOLOR 该工具必须使用root账号,否则某些项无法获取 $NOCOLOR" 
    printf '%s\n' "$ERRCOLOR 请使用 'sudo bash ${0}' 执行该程序 $NOCOLOR"
    exit 1
fi

# 判断是否是银河麒麟桌面操作系统V10SP1
if ! grep -Eqi "Kylin Linux Advanced Server" "/etc/.productinfo";then
    printf '%s\n' "$ERRCOLOR 该仅工具支持银河麒麟服务器操作系统 $NOCOLOR"
    exit 1
fi

# 判断是否是使用bash程序
if ! ps -p $$ | grep -siq bash; then
    printf '%s\n' "$ERRCOLOR 该工具仅支持使用bash使用程序 $NOCOLOR"
    printf '%s\n' "$ERRCOLOR 请使用 'sudo bash ${0}' 执行该程序 $NOCOLOR"
    exit 1
fi

KYSTL_VSERSION=0.3

# 引入需要的配置
for CONFS in $CURPATH/config/kystl-*.conf;do
    [[ -f $CONFS ]] || break
    source "$CONFS"
done

# 引入功能脚本
for SCRIPTS in $CURPATH/scripts/kystl-*.sh;do
    [[ -f $SCRIPTS ]] || break
    source "$SCRIPTS"
done

function kystl_banner()
{
    clear
    printf '%s\n'     '       _  __ __   __  ____   _____   _          '
    printf '%s\n'     '      | |/ / \ \ / / / ___| |_   _| | |         '
    printf '%s\n'     '      | ` /   \ V /  \___ \   | |   | |         '
    printf '%s\n'     '      | . \    | |    ___) |  | |   | |___      '
    printf '%s\n\n'   '      |_|\_\   |_|   |____/   |_|   |_____|     '
    printf '%s\n'     '            Kylin System Setup Tools            '
    printf '%s\n\n'   ' * Mail bug reports     : gaoruiqi@kylinos.cn   '

    sleep 1
}

function kystl_help(){
    echo "Usage: $0 "
    echo "银河麒麟系统工具"
    echo ""
    echo "kyinfo    输出银河麒麟系统相关信息"
    echo "kysec     输出银河麒麟系统相关安全信息"
}

if [ $# -eq 0 ];then
    kystl_banner
    while :;do
        kystl_kylinrepo_main_ui
    done
fi